import json
from flask import Blueprint
from flask import request
from flask import Response

from sqlalchemy import Column, String,Integer
from flask_sqlalchemy import SQLAlchemy 

from app.comm.db import db
from app.comm.db import jsonformater
from app.comm.pub import RestEntity
from app.comm.pub import class2dict

menuservice = Blueprint('menu', __name__,)


class Menu(db.Model):
    __tablename__ = 'sys_menu'
    user_id = Column(Integer, primary_key=True)
    user_name = Column(String(80), unique=True, nullable=False)
    email = Column(String(120), unique=True, nullable=False)
    def as_dict(self):
        return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
    def __repr__(self):
        return '<User %r>' % self.username


 
@menuservice.route("/rest/system/menu/list",methods=['POST','GET'])
def menuList():  
    #dict1 = json.loads(a)
    tables=db.session.execute("select menu_id menuId,menu_name menuName,parent_id parentId,order_num orderNum,url,menu_type menuType,visible,perms,icon from sys_menu").fetchall()
    datas= [dict(r) for r in tables] 
    result=RestEntity(0,0,datas,"登陆成功！")
    return json.dumps(datas, default=jsonformater)
 
    #return json.dumps(dict1)     
'''
获取用户
'''

@menuservice.route("/rest/system/menu/get",methods=['POST','GET'])
def menubyid():  
    #dict1 = json.loads(a)
    tables=db.session.execute("select menu_id menuId,menu_name menuName,parent_id parentId,order_num orderNum,url,menu_type menuType,visible,perms,icon from sys_menu").fetchall()
    datas= [dict(r) for r in tables] 
    result=RestEntity(0,0,datas,"登陆成功！")
    return json.dumps(datas, default=jsonformater)
 
    #return json.dumps(dict1)     